package cjyyp.producer;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.serialization.Serializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

public class _02生产者同步发送 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1. 创建 kafka 生产者的配置对象
        Properties properties = new Properties();
        // 2. 给 kafka 配置对象添加配置信息：bootstrap.servers
        // bootstrap.servers
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.249.135:9092");
        // key,value 序列化（必须）：key.serializer，value.serializer
        // key.serializer
        // value.serializer
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        // 3. 创建 kafka 生产者对象
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);
        // 4. todo 调用 send 方法,同步发送
        RecordMetadata metadata = kafkaProducer.send(new ProducerRecord<>("first", "cjyyp3")).get();
        System.out.println(" 主题： " + metadata.topic() + "->" + "分区：" + metadata.partition());
        // 5. 关闭资源
        kafkaProducer.close();
    }
}
